//
//  UDCheckBox.swift
//  AutoPrint
//
//  Created by David on 2017/1/20.
//  Copyright © 2017年 fatcat. All rights reserved.
//

import UIKit

protocol UDCheckBoxDelegate {
    func checkBoxDidUpdate(checkbox: UDCheckBox)
}

class UDCheckBox: UIButton {

    var id:Int!
    var delegate: UDCheckBoxDelegate?
    
    var checked:Bool {
        return _checked
    }
    
    private var _checked = false
    
    private var _checkedImage:UIImage?
    private var _uncheckedImage:UIImage?
    
    var checkedImage: UIImage? {
        get{
            return _checkedImage
        }
        set(image){
            _checkedImage = image
            if _checked {
                self.setImage(image, forState: .Normal)
                
            }
        }
    }
    
    var uncheckedImage: UIImage? {
        get{
            return _uncheckedImage
        }
        set(image){
            _uncheckedImage = image
            if !_checked {
                self.setImage(image, forState: .Normal)
                
            }
        }
    }
    
    init(point: CGPoint, id: Int) {
        super.init(frame: CGRect(origin: point, size: CGSize(width: 24, height: 24)))
        self.id = id
        self.addTarget(self, action: #selector(UDCheckBox.check), forControlEvents: .TouchUpInside)
        
    }
    
    func check(){
        _checked = !_checked
        
        if _checked {
            self.setImage(_checkedImage, forState: .Normal)
            
        }else{
            self.setImage(_uncheckedImage, forState: .Normal)
            
        }
        delegate?.checkBoxDidUpdate(self)
    }
    
    func goChecked(){
        _checked = true
        self.setImage(_checkedImage, forState: .Normal)
    }
    
    func goUnchecked(){
        _checked = false
        self.setImage(_uncheckedImage, forState: .Normal)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
